package com.partners.controller;

import com.partners.annotation.SimpleParam;
import com.partners.constant.ErrorMessageEnum;
import com.partners.constant.PermissionEnum;
import com.partners.dao.bo.ConfigDO;
import com.partners.model.ApiResult;
import com.partners.model.BusinessException;
import com.partners.service.ConfigService;
import com.partners.service.UserService;
import com.partners.utils.ApiResultUtil;
import com.partners.utils.TimestampUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 配置管理
 * @author liangyongle
 * @date 2019-01-02
 */
@RestController
@RequestMapping("config")
@Slf4j
public class ConfigController {

     @Resource
     private ConfigService configService;

     @Resource
     private UserService userService;


    /**
     * 编辑设置
     * @param configDO ConfigDO
     * @return Object
     */
     @PostMapping("edit_config")
     public ApiResult editConfig(@RequestBody ConfigDO configDO,@SimpleParam("uid") Integer uid){
         if(uid == null){
             throw BusinessException.of(ErrorMessageEnum.UID_NOT_FOUND);
         }
         Integer code = PermissionEnum.CONFIG_EDIT.getCode();
         userService.isPermission(uid, code);
         configDO.setId(1);
         configDO.setEditby(uid);
         configDO.setAddtime(TimestampUtils.timestamp());
         Integer result = configService.editConfig(configDO);
         return ApiResultUtil.ok(result);
     }


    /**
     * 获取配置信息
     * @param uid Integer
     * @return Object
     */
     @PostMapping("config")
     public ApiResult<ConfigDO> getConfig(@SimpleParam("uid") Integer uid){
         if(uid == null){
             throw BusinessException.of(ErrorMessageEnum.UID_NOT_FOUND);
         }
         Integer code = PermissionEnum.CONFIG_DETAIL.getCode();
         userService.isPermission(uid, code);
         ConfigDO config = configService.getConfigDetail();
         return ApiResultUtil.ok(config);
     }


    /**
     * 获取配置有效期
     * @param
     * @return Integer
     */
    @PostMapping("indate")
    public ApiResult<Integer> getConfigIndate(){
        ConfigDO config = configService.getConfigDetail();
        Integer indate = config.getIndate();
        return ApiResultUtil.ok(indate);
    }

}
